package com.ilancuo.thinkinjava;

/** 子类、基类初始化、销毁的顺序
 *
 * Created by ilancuo on 2017/3/10.
 */
class Characteristic {

    private String s;

    Characteristic(String s) {
        this.s = s;
        System.out.println("create Characteristic " + s);
    }

    protected void dispose() {
        System.out.println("disposing Characteristic " + s);
    }
}

class Description {

    private String s;

    Description(String s) {
        this.s = s;
        System.out.println("create Description " + s);
    }

    protected void dispose() {
        System.out.println("disposing Description " + s);
    }
}

class LivingCreature {

    private Characteristic p = new Characteristic("is alive");
    private Description d = new Description("Basic Living Creature");

    LivingCreature() {
        System.out.println("LivingCreature()");
    }

    protected void dispose() {
        System.out.println("LivingCreature dispose");
        d.dispose();
        p.dispose();
    }
}

class Animal extends LivingCreature {

    private Characteristic p = new Characteristic("has heart");
    private Description d = new Description("Animal not Vegetable");

    Animal() {
        System.out.println("Animal()");
    }

    @Override
    protected void dispose() {
        System.out.println("Animal dispose");
        d.dispose();
        p.dispose();
        super.dispose();
    }
}

class Amphibian extends Animal {

    private Characteristic p = new Characteristic("can live in water");
    private Description d = new Description("Both water and land");

    Amphibian() {
        System.out.println("Amphibian()");
    }

    @Override
    protected void dispose() {
        System.out.println("Amphibian dispose");
        d.dispose();
        p.dispose();
        super.dispose();
    }
}


public class Frog extends Amphibian {

    private Characteristic p = new Characteristic("Croak");
    private Description d = new Description("Eats Bugs");

    public Frog() {
        System.out.println("Frog()");
    }

    @Override
    protected void dispose() {
        System.out.println("Frog dispose");
        d.dispose();
        p.dispose();
        super.dispose();
    }

    public static void main(String[] args) {
        Frog frog = new Frog();
        System.out.println("Bye!");
        frog.dispose();
    }
}
